﻿using System;
using Communication.Message.Buffer;
using Communication.Message.Util;

namespace Communication.Message.FpgaUpdate
{
	// Token: 0x02000010 RID: 16
	public abstract class FlashUpdateChannelMessage : AbstractChannelMessage
	{
		// Token: 0x17000001 RID: 1
		// (get) Token: 0x060000C7 RID: 199 RVA: 0x00002D4C File Offset: 0x00000F4C
		public FlashUpdateErrorCode ErrorCode
		{
			get
			{
				return this.errorCode;
			}
		}

		// Token: 0x17000002 RID: 2
		// (get) Token: 0x060000C8 RID: 200 RVA: 0x00002D54 File Offset: 0x00000F54
		public byte DesId
		{
			get
			{
				return this.desId;
			}
		}

		// Token: 0x17000003 RID: 3
		// (get) Token: 0x060000C9 RID: 201 RVA: 0x00002D5C File Offset: 0x00000F5C
		public byte FunCode
		{
			get
			{
				return this.funCode;
			}
		}

		// Token: 0x060000CA RID: 202 RVA: 0x00002D64 File Offset: 0x00000F64
		protected FlashUpdateChannelMessage(byte desId, byte funCode, byte[] datas, int dataLen, int offset, int totalRecTimeoutMillis, int spaceRecTimeoutMillis, int sendTimeoutMillis) : base(totalRecTimeoutMillis, spaceRecTimeoutMillis, sendTimeoutMillis, ChannelMessageType.Client)
		{
			this.sendBuffer = new DynamicMessageBuffer(8 + dataLen);
			this.receiveBuffer = new DynamicMessageBuffer(10);
			this.desId = desId;
			this.funCode = funCode;
			this.errorCode = FlashUpdateErrorCode.UNKONWN;
			this.SetPayload(datas, offset, dataLen);
		}

		// Token: 0x060000CB RID: 203 RVA: 0x00002DC0 File Offset: 0x00000FC0
		public virtual void SetPayload(byte[] sendData, int offset, int length)
		{
			if (length > 0)
			{
				if (sendData == null)
				{
					throw new ArgumentNullException();
				}
				if (offset < 0 || offset + length > sendData.Length)
				{
					throw new IndexOutOfRangeException();
				}
			}
			this.sendBuffer.Clear();
			this.receiveBuffer.Clear();
			this.sendBuffer.WriteByte(253);
			this.sendBuffer.WriteByte(this.desId);
			this.sendBuffer.WriteByte(this.funCode);
			this.sendBuffer.WriteShort((short)length);
			if (length > 0)
			{
				for (int i = 0; i < length; i++)
				{
					this.sendBuffer.WriteByte(sendData[offset + i]);
				}
			}
			short s = CrcUtil.Crc16(this.sendBuffer.Array(), this.sendBuffer.ReaderIndex(), this.sendBuffer.ReadableBytes(), CrcType.High_Low);
			this.sendBuffer.WriteShort(s);
			this.sendBuffer.WriteByte(13);
			this.Reset();
		}

		// Token: 0x060000CC RID: 204 RVA: 0x00002EA8 File Offset: 0x000010A8
		public override int GetSendData(byte[] buffer, int offset)
		{
			if (buffer == null)
			{
				throw new ArgumentNullException();
			}
			if (offset < 0 || offset > buffer.Length)
			{
				throw new IndexOutOfRangeException();
			}
			int num = this.sendBuffer.ReadBytes(buffer, offset);
			FlashUpdateChannelMessage.totalSendByte += (long)num;
			return num;
		}

		// Token: 0x060000CD RID: 205 RVA: 0x00002EEA File Offset: 0x000010EA
		public override int GetCancelSendData(byte[] buffer, int offset)
		{
			return 0;
		}

		// Token: 0x060000CE RID: 206 RVA: 0x00002EED File Offset: 0x000010ED
		public override int FillReceivedData(byte[] receivedDatas, int offset, int length)
		{
			if (receivedDatas == null)
			{
				throw new ArgumentNullException();
			}
			if (offset < 0 || offset + length > receivedDatas.Length)
			{
				throw new IndexOutOfRangeException();
			}
			FlashUpdateChannelMessage.totalReceiveByte += (long)length;
			this.receiveBuffer.WriteBytes(receivedDatas, offset, length);
			this.DealwithReceiveMsg();
			return length;
		}

		// Token: 0x060000CF RID: 207
		protected abstract bool DealwithReceiveMsg();

		/// <summary>
		/// 判断是否是合法报文
		/// </summary>
		/// <returns></returns>
		protected bool IdentifyFrame()
		{
			while (this.receiveBuffer.ReadableBytes() >= 8)
			{
				if (this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex()) != 253)
				{
					this.receiveBuffer.ReadByte();
				}
				else
				{
					if (this.receiveBuffer.ReadableBytes() < (int)(this.receiveBuffer.GetUshort(this.receiveBuffer.ReaderIndex() + 3) + 8)) //长度不都
					{
						return false;
					}
					if (this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex()) == 253 && this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex() + 1) == this.desId && this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex() + 2) == this.funCode && this.receiveBuffer.GetByte(this.receiveBuffer.ReaderIndex() + 7 + (int)this.receiveBuffer.GetUshort(this.receiveBuffer.ReaderIndex() + 3)) == 13)
					{
						return true;
					}
					this.receiveBuffer.ReadByte();
				}
			}
			return false;
		}

		// Token: 0x060000D1 RID: 209 RVA: 0x0000304B File Offset: 0x0000124B
		protected override void Reset()
		{
			base.Reset();
			this.errorCode = FlashUpdateErrorCode.UNKONWN;
		}

		// Token: 0x060000D2 RID: 210 RVA: 0x0000305E File Offset: 0x0000125E
		public string HexDumpSendMessage()
		{
			return MessageBuffers.HexDump(this.sendBuffer, 0, this.sendBuffer.WriterIndex());
		}

		// Token: 0x060000D3 RID: 211 RVA: 0x00003077 File Offset: 0x00001277
		public string HexDumpReceiveMessage()
		{
			return MessageBuffers.HexDump(this.receiveBuffer);
		}

		// Token: 0x04000012 RID: 18
		public static long totalSendByte = 0L;

		// Token: 0x04000013 RID: 19
		public static long totalReceiveByte = 0L;

		// Token: 0x04000014 RID: 20
		protected FlashUpdateErrorCode errorCode;

		// Token: 0x04000015 RID: 21
		protected MessageBuffer sendBuffer;

		// Token: 0x04000016 RID: 22
		protected MessageBuffer receiveBuffer;

		// Token: 0x04000017 RID: 23
		private byte desId;

		// Token: 0x04000018 RID: 24
		private byte funCode;
	}
}
